<template>
  <div>
    <div class="min-container">
      <div class="content_top">
        <div class="title">{{ marketInfo.title || "无" }}</div>
        <div class="con" v-html="marketInfo.intro"></div>
        <div class="core">
          <img :src="baseURL + marketInfo.picture" class="banner" />
          <div class="middle-title">
            <span v-if="firstSkuList.length">规格：</span>
            <span v-if="payMethodList.length">购买方式：</span>
            <span>购买时长：</span>
          </div>
          <div class="middle-con">
            <div class="specifications" v-if="firstSkuList.length">
              <button
                v-for="(i, k) in firstSkuList"
                :key="k"
                class="btn1"
                :class="{ active: i == firstActive }"
                @click="handleFirstSku(i)"
              >
                {{ i }}
              </button>
            </div>
            <div class="specifications" v-if="payMethodList.length">
              <button
                v-for="(i, k) in payMethodList"
                :key="k"
                class="btn1"
                :class="{ active: i.cloudMarketBuyingMethod == payActive }"
                @click="handlePayMethod(i)"
              >
                {{ ChargeType(i.cloudMarketBuyingMethod) }}
              </button>
            </div>
            <div class="purchaseDuration">
              <el-input-number
                v-model="periodNum"
                :min="1"
                step-strictly
                @change="setPrice"
              />
              <i style="margin-left: 20px; margin-right: 50px; text-align: center">{{
                unit
              }}</i>
              <div v-if="'0' != payActive">
                <el-checkbox v-model="autoRenew" label="自动续费" size="large" />
                <el-tooltip effect="light" placement="top">
                  <template #content>
                    按天购买：自动续费周期为1天<br />按月购买：自动续费周期为1个月<br />按年购买：自动续费周期为1年
                  </template>
                  <el-icon :size="16" color="#ccc"><QuestionFilled /></el-icon>
                </el-tooltip>
              </div>
            </div>
          </div>
        </div>
        <div
          style="
            width: 100%;
            text-align: center;
            display: flex;
            justify-content: center;
            align-items: center;
          "
        >
          <el-checkbox v-model="readAgreement" label="我已阅读并同意 " size="large" />
          <i style="color: #4098ff; cursor: pointer" @click="handleAgreement"
            >《中酒云网云市场用户协议》</i
          >
        </div>
        <div class="priceCon">
          <div class="allPrice">
            价格总计
            <span>{{ price }}</span>
          </div>
          <div class="allPriceBtn">
            <button @click="handleConfirm">加入购物车</button>
          </div>
        </div>
      </div>
      <div class="content_con">
        <div class="title_top">
          <img src="@/assets/img/client/cloudService/Group2248.png" />
        </div>
        <div class="manufacturer">
          <div>
            <img :src="baseURL + marketInfo.corporateImg" />
          </div>
          <div class="manufacturer-right">
            <span>{{ marketInfo.corporateName || "无" }}</span>
            <span>服务电话：{{ marketInfo.servicePhone || "无" }}</span>
            <span>服务邮箱：{{ marketInfo.serviceEmail || "无" }}</span>
          </div>
        </div>
        <div class="product">
          <div>
            <img src="@/assets/img/client/cloudService/Group2233.png" />
          </div>
        </div>
        <div v-html="marketInfo.description" />
      </div>
    </div>
    <contactUs />
    <Agreement-modal v-model:show="agreementShow" @confirm="readAgreement = true" />
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { useRoute } from "vue-router";
import contactUs from "@/views/unit/contactUs.vue";
const route = useRoute();

import {
  marketDetail,
  marketFirstDirectory,
  marketBuyingMethod,
} from "@/api/other.js";
import { shoppingCar } from "@/api/cloud/index";

const baseURL = import.meta.env.VITE_BASE_URL;
const marketId = ref<any>(""); // 云市场id
const marketInfo = ref<any>({}); // 云市场信息
const firstSkuList = ref<any>([]); // 一级分类
const firstActive = ref(""); // 一级分类选中
const payMethodList = ref<any>([]); // 付费方式
const payActive = ref(); // 付费方式选中
const periodNum = ref(1); // 购买周期
const unit = ref(""); // 单位
const price = ref(0); // 金额
const autoRenew = ref(false); // 是否自动续费
const readAgreement = ref(false); // 是否阅读协议
const agreementShow = ref(false); // 协议弹窗
const isClick = ref(false); // 防抖
const discount = ref(10); //折扣

onMounted(() => {
  marketId.value = route.query.marketId;
  getMarketDetail();
  getMarketFirstDirectory();
});

// 获取云市场详情
const getMarketDetail = () => {
  marketDetail(marketId.value).then((res: any) => {
    if (res.code === 200) {
      marketInfo.value = res.data;
      marketInfo.value.intro = marketInfo.value.intro.replaceAll(
        "<img",
        '<img style="width: 100px;height: 100px;"'
      );
    }
  });
};

// 获取云市场一级规格
const getMarketFirstDirectory = () => {
  marketFirstDirectory(marketId.value).then((res: any) => {
    if (res.code === 200) {
      firstSkuList.value = res.data;
      if (res.data.length > 0) {
        handleFirstSku(res.data[0]);
      }
    }
  });
};

// 选择一级规格
const handleFirstSku = (val: any) => {
  firstActive.value = val;
  getMarketBuyingMethod();
};

// 获取云市场购买方式
const getMarketBuyingMethod = () => {
  let data = {
    marketId: marketId.value,
    firstSkuName: firstActive.value,
  };
  marketBuyingMethod(data).then((res: any) => {
    if (res.code === 200) {
      payMethodList.value = res.data;
      if (res.data.length > 0) {
        handlePayMethod(res.data[0]);
      }
    }
  });
};

// 选择购买方式
const handlePayMethod = (val: any) => {
  payActive.value = val.cloudMarketBuyingMethod;
  setPrice();
  if (payActive.value == 0) {
    unit.value = "次";
  } else if (payActive.value == 1) {
    unit.value = "天";
  } else if (payActive.value == 30) {
    unit.value = "月";
  } else if (payActive.value == 365) {
    unit.value = "年";
  }
};

// 购买方式中文
const ChargeType = (val: number) => {
  if (0 == val) {
    return "按次";
  } else if (1 == val) {
    return "按天";
  } else if (30 == val) {
    return "按月";
  } else if (365 == val) {
    return "按年";
  }
};

// 设置金额
const setPrice = () => {
  let row = payMethodList.value.find((i: any) => i.cloudMarketBuyingMethod == payActive.value);
  price.value = Number(row.price || 0) * periodNum.value * (discount.value / 10);
};

// 提交订单
const handleConfirm = () => {
  if (!readAgreement.value) {
    ElMessage.warning("请阅读并同意《用户协议》");
    return;
  }
  if (isClick.value) return;
  isClick.value = true;
  let row = payMethodList.value.find((i: any) => i.cloudMarketBuyingMethod == payActive.value);
  if (row) {
    let data = {
      shoppingCarEnums: "CLOUD_MARKET",
      relationId: row.marketSkuId,
      periodNum: periodNum.value,
      autoRenew: autoRenew.value,
    };
    shoppingCar(data)
      .then((res: any) => {
        if (res.code == 200) {
          ElMessage.success("加入购物车成功,请前往控制台查看");
        }
      })
      .finally(() => {
        isClick.value = false;
      });
  } else {
    ElMessage.warning("暂无规格，无法购买");
  }
};

// 打开协议弹窗
const handleAgreement = () => {
  agreementShow.value = true;
};
</script>
<style lang="less" scoped>
.content_top {
  width: 100%;
  background: #ffffff;
  margin: 20px 0;
  padding: 20px;
  .title {
    width: 100%;
    height: 60px;
    font-size: 18px;
    font-weight: 600;
    color: #313131;
    line-height: 60px;
    border-bottom: 1px solid #e3e8f0;
    margin-bottom: 20px;
  }
  .con {
    font-size: 14px;
    font-weight: 400;
    line-height: 30px;
    text-align: left;
    width: 100%;
    border-bottom: 1px solid #e3e8f0;
  }
  .sku-row {
    margin: 20px 0 0;
  }
}
.core {
  width: 100%;
  display: flex;
  .banner {
    margin-top: 20px;
    width: 292px;
    height: 162px;
  }
  .middle-title {
    width: 90px;
    height: 162px;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    margin-left: 20px;
    font-size: 14px;
    font-weight: 400;
    text-align: left;
    letter-spacing: 2px;
    color: #909090;
    margin-top: 30px;
  }
  .middle-con {
    margin-top: 30px;
    margin-left: 30px;
    height: 162px;
    justify-content: space-around;
    display: flex;
    flex-direction: column;
  }
  .specifications {
    display: flex;
  }
  .purchaseDuration {
    color: #909090;
    display: flex;
    align-items: center;
    .purchaseDuration-left {
      width: 178px;
      height: 38px;
      border: 1px solid #d9d9d9;
    }
  }
}
.priceCon {
  width: 100%;
  height: 100px;
  background: url(@/assets/img/client/cloudService/Rectangle1495.png) no-repeat center
    center / 100% 100%;
  display: flex;
  .allPrice {
    display: flex;
    flex: 2;
    color: #000;
    font-size: 14px;
    align-items: center;
    justify-content: right;
    > span {
      font-weight: 500;
      font-size: 36px;
      padding: 0 50px;
      color: #ff3939;
      &::before {
        content: "￥";
        font-size: 80%;
      }
    }
  }
  .allPriceBtn {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    button {
      width: 126px;
      height: 40px;
      border: none;
      background: #4098ff;
      color: #fff;
      cursor: pointer;
    }
  }
}
.content_con {
  width: 100%;
  background: #fff;
  padding: 20px;
  margin-bottom: 40px;
  .title_top {
    width: 100%;
    height: 140px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom: 1px solid #d9d9d9;
    img {
      width: 214px;
      height: 94px;
    }
  }
  .manufacturer {
    width: 100%;
    height: 300px;
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    img {
      width: 367px;
      height: 207px;
    }
    .manufacturer-right {
      height: 100px;
      display: flex;
      font-size: 16px;
      font-weight: 400;
      flex-direction: column;
      justify-content: space-between;
    }
  }
  .product {
    width: 100%;
    height: 140px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom: 1px solid #d9d9d9;
    margin-bottom: 20px;
    img {
      width: 214px;
      height: 94px;
    }
  }
  .coreFunctions {
    width: 100%;
    height: 140px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom: 1px solid #d9d9d9;
    margin-bottom: 20px;
    img {
      width: 214px;
      height: 94px;
    }
  }
  .advantage {
    width: 100%;
    height: 140px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 20px;
    img {
      width: 214px;
      height: 94px;
    }
  }
  .itemList {
    width: 100%;
    height: 152px;
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    padding: 20px;
    background: url(@/assets/img/client/cloudService/Rectangle1499.png) no-repeat center
      center / 100% 100%;
    img {
      width: 69px;
      height: 69px;
      margin-left: 30px;
      margin-right: 60px;
    }
    .itemCon {
      height: 100%;
      display: flex;
      line-height: 30px;
      flex-direction: column;
      justify-content: space-evenly;
    }
  }
}
.contact {
  width: 100%;
  img {
    width: 100%;
  }
}
.btn1 {
  min-width: 64px;
  height: 36px;
  color: #b2b2b2;
  border: 1px solid #d9d9d9;
  background: #fbfbfb;
  margin-right: 10px;
  cursor: pointer;
}
.active {
  color: #4098ff;
  border: 1px solid #4098ff;
}
</style>
